package com.febers.ihealth

import android.view.View
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import com.afollestad.materialdialogs.DialogCallback
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.list.listItems
import com.febers.ihealth.base.BaseActivity
import com.febers.ihealth.utils.AnalyzeUtil
import com.febers.ihealth.utils.nav
import com.febers.ihealth.utils.toast
import com.febers.ihealth.utils.user
import kotlinx.android.synthetic.main.activity_nav.*
import kotlinx.android.synthetic.main.toolbar_common.*

class NavActivity: BaseActivity() {

    override fun setView(): Int = R.layout.activity_nav

    override fun setToolbar(): Toolbar? = toolbar_common

    override fun setToolbarTitle(): String? = "导航"

    override fun initView() {
        btn_test_analyze.visibility = View.GONE
    }

    override fun afterCreate() {
        tv_nav_welcome.text = "欢迎您，${user.name}"
        btn_into_base_survey.setOnClickListener {
            nav(BaseInfoActivity::class.java)
        }
        btn_into_normal_survey.setOnClickListener {
            if (!user.baseInfoFinish()) {
                toast("请您先填写基本信息", Toast.LENGTH_LONG)
                nav(BaseInfoActivity::class.java)
            } else {
                nav(BeforeSurveyAct::class.java)
            }
        }
        btn_into_login.setOnClickListener {
            MaterialDialog(this)
                .title(text = "是否注销登录")
                .cancelable(false)
                .show{
                    positiveButton(text = "注销") {
                        user.clearBaseInfo()
                        nav(LoginActivity::class.java)
                        finish()
                    }
                    negativeButton(text = "取消") {
                        it.dismiss()
                    }
                }
        }
        btn_test_analyze.setOnClickListener {
            val map = AnalyzeUtil.analysis()
            MaterialDialog(this)
                .title(text = "分析结果")
                .listItems(items = map)
                .positiveButton(text = "确定", click = object : DialogCallback {
                    override fun invoke(p1: MaterialDialog) {
                        p1.dismiss()
                    }
                })
                .cancelOnTouchOutside(false)
                .show()
        }
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (user.baseInfoFinish()) {
            btn_into_base_survey.text = "更新基本信息"
        } else {
            btn_into_base_survey.text = "填写基本信息"
        }
    }
}